package com.czl.module_storehouse.activity.receive.add;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_service.event.ReceiveAddEvent;
import com.czl.module_service.event.StorehouseHomeScanBackEvent;
import com.czl.module_storehouse.activity.base.BaseOperationActivity;
import com.czl.module_storehouse.adapter.ReceiveAddAdapter;
import com.czl.module_storehouse.bean.ReceiveInfoBean;
import com.czl.module_storehouse.bean.WorkGroupBean;
import com.czl.module_storehouse.bean.WorkerBean;
import com.czl.module_storehouse.databinding.ActivityBaseOperationBinding;
import com.czl.module_storehouse.databinding.HeaderReceiveAddBinding;
import com.czl.module_storehouse.listener.OnItemChangeListener;
import com.czl.module_storehouse.mvp.presenter.CompanyPresenter;
import com.czl.module_storehouse.mvp.presenter.ProductPresenter;
import com.czl.module_storehouse.mvp.presenter.ReceivePresenter;
import com.czl.module_storehouse.mvp.view.ReceiveView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class ReceiveAddActivity extends BaseOperationActivity<ActivityBaseOperationBinding> implements OnItemChangeListener,
        ReceiveView, SimpleView {

    private final ReceiveInfoBean mReceiveInfoBean = new ReceiveInfoBean();
    private ReceiveAddAdapter mAdapter;
    private HeaderReceiveAddBinding mHeaderBinding;
    @InjectPresenter
    ReceivePresenter mReceivePresenter;
    @InjectPresenter
    CompanyPresenter mCompanyPresenter;
    @InjectPresenter
    ProductPresenter mProductPresenter;

    @Override
    protected ProductPresenter getProductPresenter() {
        return mProductPresenter;
    }

    @Override
    protected ActivityBaseOperationBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityBaseOperationBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mHeaderBinding = HeaderReceiveAddBinding.inflate(getLayoutInflater());
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("领用单");
        binding.recyclerView.setAdapter(getAdapter());
        mAdapter.addHeaderView(mHeaderBinding.getRoot());
        mCommonSelectedOrScanBinding.textSelected.setText("选择领用物品");
        mCommonSelectedOrScanBinding.textScan.setText("扫码领用物品");
        mCommonSelectedOrganizationBinding.stvOrganization.setLeftString("领用部门*");
        mCommonSelectedOrganizationBinding.stvPersonnel.setLeftString("领用人*");
        binding.btnSure.setText("领用出库");
    }

    @Override
    protected void loadData() {
        super.loadData();
    }

    @Override
    protected ViewGroup selectedOrganizationView() {
        return mHeaderBinding.flContent;
    }

    @Override
    protected ViewGroup selectedScanContainerView() {
        return mHeaderBinding.flAdd;
    }

    @Override
    protected void submit() {

        if (TextUtils.isEmpty(mOrganizationId)) {
            if (mWorkerBean == null) {
                showToast("请选择领用部门");
                return;
            }
        }
        if (mWorkerBean == null) {
            showToast("请选择领用人");
            return;
        }
        mReceiveInfoBean.setReceivrOrganizationId(Integer.parseInt(mOrganizationId));
        mReceiveInfoBean.setReceivePersonId(mWorkerBean.getUserId());
        mReceiveInfoBean.setReceivePersonName(mWorkerBean.getTname());
        List<SortBean> list = new ArrayList<>();
        List<ProductBean> productList = new ArrayList<>();
        for (SortBean sortBean : mAdapter.getData()) {
            if (sortBean.getProductBean() == null) {
                list.add(sortBean);
            } else {
                productList.add(sortBean.getProductBean());
            }
        }
        mReceiveInfoBean.setSortList(list);
        mReceiveInfoBean.setProductList(productList);
        String remark = mFooterBinding.etRemark.getText().toString();
        mReceiveInfoBean.setReceiveComment(remark);
        String desc = mHeaderBinding.etDeptDesc.getText().toString();
        mReceiveInfoBean.setApplyComment(desc);
        if (mReceivePresenter != null) {
            mReceivePresenter.addReceiveInfo(mReceiveInfoBean);
        }
    }

    @Override
    protected BaseQuickAdapter<SortBean, ?> getAdapter() {
        if (mAdapter == null) {
            mAdapter = new ReceiveAddAdapter(new ArrayList<>(), this);
            mAdapter.setOnItemChildClickListener((adapter, view, position) -> startActivity(position));
            addFooterView(mAdapter);
        }
        return mAdapter;
    }

    @Override
    protected Button getButton() {
        return binding.btnSure;
    }

    @Override
    protected String getOperationTitle() {
        return "领用";
    }

    @Override
    protected int getMaxNum(SortBean sortBean) {
        return Integer.MAX_VALUE;
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (CompanyPresenter.TAG_COMPANY_GROUP.equals(httpResponse.getRequestTag())) {
            addChildWorkGroup(((ListBean<WorkGroupBean>) httpResponse.getData()).getList());
            showPopupWindowGroup();
        } else if (CompanyPresenter.TAG_COMPANY_USER_LIST.equals(httpResponse.getRequestTag())) {
            mWorkerBeanList = ((ListBean<WorkerBean>) httpResponse.getData()).getList();
            showPopupWindowUsers();
        } else if (ReceivePresenter.TAG_RECEIVE_ADD_INFO.equals(httpResponse.getRequestTag())) {
            if (homeScan) {
                LiveEventBus.get(StorehouseHomeScanBackEvent.class).post(new StorehouseHomeScanBackEvent());
            } else {
                LiveEventBus.get(ReceiveAddEvent.class).post(new ReceiveAddEvent(true, null));
            }
            showToast("领用成功");
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void onChange(int position) {
        setButtonState();
    }

    @Override
    public void add(int position) {
        setButtonState();
    }

    @Override
    public void remove(int position) {
        setButtonState();
    }

    @Override
    protected void selectOrganization() {
        mCompanyPresenter.selectOrganization();
    }

    @Override
    protected void selectUsersByCompanyId() {
        super.selectUsersByCompanyId();
        mCompanyPresenter.selectUsersByCompanyId(mOrganizationId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
    }
}
